package com.gkd.controller;

import com.gkd.common.QueryPageParam;
import com.gkd.common.Result;
import com.gkd.mapper.StorageMapper;
import com.gkd.pojo.Storage;
import com.gkd.pojo.StoragePage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class StorageController {
    @Autowired
    private StorageMapper storageMapper;

    //分页列表
    @PostMapping("/listStoragePage")
    public Result listStoragePage(@RequestBody QueryPageParam query) {
        int pageNum = query.getPageNum() - 1;
        int pageSize = query.getPageSize();

        List<Storage> storages = storageMapper.getPageStorage(pageNum * pageSize, pageSize);
        if (storages != null) {
            return Result.result(200, "获取数据成功", storageMapper.getAllStorage().size(), storages);
        }
        return Result.fail();
    }

    //新增数据
    @PostMapping("/addStorage")
    public Result addStorage(@RequestBody Storage storage) {
        Storage isStorage = storageMapper.getByNameStorageOne(storage.getName());
        if (isStorage != null) {
            //如果用户已经存在
            return Result.addFail(storageMapper.getAllStorage().size(), "仓库已存在");
        }

        int i = storageMapper.addStorage(storage);
        if (i > 0) {
            return Result.addSuc(storageMapper.getAllStorage().size());
        }
        return Result.addFail(storageMapper.getAllStorage().size(), "新增仓库失败");
    }

    //删除数据
    @PostMapping("/deleteStorage")
    public Result deleteStorage(@RequestBody Storage storage) {
        int id = storage.getId();
        int i = storageMapper.deleteStorage(id);
        if (i > 0) {
            return Result.deleteSuc(storageMapper.getAllStorage().size());
        }
        return Result.deleteFail(storageMapper.getAllStorage().size());
    }

    //修改数据
    @PostMapping("/updateStorage")
    public Result updateStorage(@RequestBody Storage storage) {
        Storage byIdStorage = storageMapper.getByIdStorage(storage.getId());

        Storage isStorage = storageMapper.getByNameStorageOne(storage.getName());
        if (!storage.getName().equals(byIdStorage.getName()) && isStorage != null) {
            //如果仓库已经存在
            return Result.updateFail(storageMapper.getAllStorage().size(), "仓库已存在");
        }
        int i = storageMapper.updateStorage(storage);
        if (i > 0) {
            return Result.updateSuc(storageMapper.getAllStorage().size());
        }
        return Result.updateFail(storageMapper.getAllStorage().size(), "修改仓库失败");
    }

    //模糊查询
    @PostMapping("/likeStoragePage")
    public Result likeStoragePage(@RequestBody StoragePage storagePage) {
        int pageNum = storagePage.getPageNum() - 1;
        int pageSize = storagePage.getPageSize();

        String name = storagePage.getName();
        if (!name.equals("")) {
            List<StoragePage> storagePages = storageMapper.getByNamePageStorage(name, pageNum * pageSize, pageSize);
            return Result.result(200, "查询仓库成功", storageMapper.getByNameStorage(name).size(), storagePages);
        }
        return Result.fail();
    }
}
